//
//  GYLoginVC.m
//  GYDemo
//
//  Created by Mrzhang on 2018/3/22.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYLoginVC.h"
#import "GYRegisterVC.h"
#import "Head.h"
#define unshowImage           [UIImage imageNamed:@"browse_fill"]
#define showImage             [UIImage imageNamed:@"browse_fill (1)"]

@interface GYLoginVC ()<UITextFieldDelegate>
@property (nonatomic,strong) UIImageView *logIV;
@property (nonatomic,strong) UIImageView *userIV;
@property (strong, nonatomic) UITextField *userTF;
@property (nonatomic,strong) UIView *sep1;
@property (nonatomic,strong) UIImageView *passwordIV;
@property (strong, nonatomic) UITextField *passwordTF;
@property (nonatomic,strong) UIView *sep2;
@property (nonatomic,strong) UIButton *loginBtn;
@property (nonatomic,strong) UIButton *registBtn;
@property (nonatomic,strong) UIButton *forgetBtn;
@property (nonatomic,strong) UIButton *showPswBtn;


@property(nonatomic,strong)GYLoginViewModel* loginViewModel;
@end

@implementation GYLoginVC
#pragma mark - getter and setter

-(GYLoginViewModel *)loginViewModel
{
    if (!_loginViewModel) {
        _loginViewModel=[GYLoginViewModel shareInterface];
    }
    return _loginViewModel;
}
-(UIButton *)showPswBtn
{
    if (!_showPswBtn) {
        _showPswBtn = [[UIButton alloc]init];

        
        _showPswBtn.frame =  WFCGRectMake(54+275, 104, unshowImage.size.width, unshowImage.size.height);
        [_showPswBtn setBackgroundImage:unshowImage forState:UIControlStateNormal];
        [_showPswBtn setBackgroundImage:showImage forState:UIControlStateSelected];
        [_showPswBtn addTarget:self action:@selector(shwoPsw:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _showPswBtn;
}
-(UIImageView *)logIV
{
    if (!_logIV) {
        _logIV = [[UIImageView alloc]init];
        _logIV.image = [UIImage imageNamed:@"mobilephone_fill"];
        _logIV.frame = WFCGRectMake(27, 50, _logIV.image.size.width,  _logIV.image.size.height);
    }
    return _logIV;
}
-(UIImageView *)userIV
{
    if (!_userIV) {
        _userIV = [[UIImageView alloc]init];
        _userIV.image = [UIImage imageNamed:@"mobilephone_fill"];
        _userIV.frame = WFCGRectMake(27, 50, _userIV.image.size.width,  _userIV.image.size.height);
        
    }
    return _userIV;
}
-(UITextField *)userTF
{
    if (!_userTF) {
        _userTF = [[UITextField alloc]init];
        _userTF.frame = WFCGRectMake(54, 40, 300, 38);
        _userTF.placeholder = @"手机号";
        _userTF.keyboardType = UIKeyboardTypePhonePad;
        _userTF.font = SYSTEMFONT(15);
        _userTF.textColor = Main_Color_Black;
        _userTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        //        [WFFunctions WFSysddFinishButtonForText:_userTF withSelector:@selector(tapFinish) andTarget:self];
        _userTF.delegate = self;
    }
    return _userTF;
}
-(UIView *)sep1
{
    if (!_sep1) {
        _sep1 = [[UIView alloc]init];
        _sep1.frame = WFCGRectMake(20, 82.5, 335, 1);
        _sep1.backgroundColor = Main_Color_BGLightGray;
    }
    return _sep1;
}

-(UIImageView *)passwordIV
{
    if (!_passwordIV) {
        _passwordIV = [[UIImageView alloc]init];
        _passwordIV.image = [UIImage imageNamed:@"lock_fill"];
        _passwordIV.frame = WFCGRectMake(27, 100, _passwordIV.image.size.width, _passwordIV.image.size.height);
        
    }
    return _passwordIV;
}
-(UITextField *)passwordTF
{
    if (!_passwordTF) {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.frame = WFCGRectMake(54, 90, 270, 38);
        _passwordTF.placeholder = @"密码";
        _passwordTF.font = SYSTEMFONT(15);
        _passwordTF.textColor = Main_Color_Black;
        _passwordTF.secureTextEntry = YES;
        _passwordTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        _passwordTF.delegate = self;
    }
    return _passwordTF;
}
-(UIView *)sep2
{
    if (!_sep2) {
        _sep2 = [[UIView alloc]init];
        _sep2.frame = WFCGRectMake(20, 132.5, 335, 1);
        _sep2.backgroundColor = Main_Color_BGLightGray;
    }
    return _sep2;
}

-(UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc]init];
        _loginBtn.frame = WFCGRectMake(20, 149, 335, 45);
        [_loginBtn setBackgroundImage:[WFFunctions WFUICreateImageWithColor:RGBACOLOR(221, 221, 221, 1)] forState:UIControlStateDisabled];
        [_loginBtn addTarget:self action:@selector(loginTap:) forControlEvents:UIControlEventTouchUpInside];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:RGBACOLOR(155, 155, 155, 1) forState:UIControlStateDisabled];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[WFFunctions WFUICreateImageWithColor:Main_Color_Red] forState:UIControlStateNormal];
        //_loginBtn.enabled=NO;
        [WFFunctions WFUIaddbordertoView:_loginBtn radius:4 width:0 color:[UIColor clearColor]];
        _loginBtn.titleLabel.font = SYSTEMFONT(15);
    }
    return _loginBtn;
}
-(UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [[UIButton alloc]init];
        _registBtn.frame = WFCGRectMake(12, 204, 50, 30);
        [_registBtn addTarget:self action:@selector(registTab:) forControlEvents:UIControlEventTouchUpInside];
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registBtn setTitleColor:Main_Color_Gray forState:UIControlStateNormal];
        _registBtn.titleLabel.font = SYSTEMFONT(14);
    }
    return _registBtn;
}
-(UIButton *)forgetBtn
{
    if (!_forgetBtn)
    {
        _forgetBtn = [[UIButton alloc]init];
        _forgetBtn.frame = WFCGRectMake(280, 204, 75, 30);
        [_forgetBtn addTarget:self action:@selector(registTab:) forControlEvents:UIControlEventTouchUpInside];
        [_forgetBtn setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetBtn setTitleColor:RGBACOLOR(184, 26, 8, 0.59) forState:UIControlStateNormal];
        _forgetBtn.titleLabel.font = SYSTEMFONT(14);
    
        _forgetBtn.tag=200;
        
    }
    return _forgetBtn;
}
#pragma mark - event
-(void)loginTap:(id)sender
{
    
    [UIApplication sharedApplication].delegate.window.rootViewController = [GCPresenter shareInterface].mainVC;
    self.loginViewModel.username = _userTF.text;
    self.loginViewModel.password = _passwordTF.text;
    [self.loginViewModel requsetLogin];
}
-(void)registTab:(UIButton*)sender
{
    GYRegisterVC* gy=[[GYRegisterVC alloc] init];
    if (sender.tag==200) {
        gy.isForget=YES;
    }
    [self.navigationController pushViewController:gy animated:YES];
}
-(void)shwoPsw:(UIButton*)sender
{
    _passwordTF.secureTextEntry=sender.selected;
    sender.selected=!sender.selected;
    
}
#pragma mark - method

-(void)initilization
{
    [self setNavgationTheme:1];
    self.navigationItem.title=@"登录";
    [self bingObserve];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"logclose"] style:UIBarButtonItemStylePlain target:self action:@selector(dismissVC)];
}
-(void)dismissVC
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)bingObserve
{
    [self.loginViewModel addObserver:self forKeyPath:@"loginInfoModel" options:NSKeyValueObservingOptionNew||NSKeyValueChangeOldKey context:@"loginInfoModel"];
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [self initilization];
    [self.view addSubview:self.userIV];
    [self.view addSubview:self.userTF];
    [self.view addSubview:self.passwordIV];
    [self.view addSubview:self.passwordTF];
    [self.view addSubview:self.showPswBtn];
    [self.view addSubview:self.sep1];
    [self.view addSubview:self.sep2];
    [self.view addSubview:self.loginBtn];
    [self.view addSubview:self.registBtn];
    [self.view addSubview:self.forgetBtn];
    _userTF.text = self.loginViewModel.username;
    // Do any additional setup after loading the view.
}
-(void)dealloc
{
    [self.loginViewModel removeObserver:self forKeyPath:@"loginInfoModel"];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"loginInfoModel"]) {
        [self dismissViewControllerAnimated:YES completion:nil];
        
    }
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (self.userTF.text.length>0&&self.passwordTF.text.length>4) {
        self.loginBtn.enabled=YES;
    }else
    {
        self.loginBtn.enabled=NO;
    }
    return YES;
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
